﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Diagnostics;

namespace PetriNetSharp.Converter
{
	/// <summary>
	/// Converter for converting a string representing seconds to a timespan
	/// </summary>
	public class SecondStringToTimeSpanValueConverter : IValueConverter
	{
		/// <summary>
		/// Convert a object's whose to string representing seconds to a timespan
		/// </summary>
		/// <param name="value">An object whose to string is an integer</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>A Timespan for the seconds</returns>
		/// <preconditions>
		/// value's ToString() must an integer
		/// </preconditions>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			int seconds;

			#region preconditions
			Debug.Assert(int.TryParse(value.ToString(), out seconds), 
				"SecondStringToTimeSpan convert value was not an integer",
				"SecondStringToTimeSpan had Convert called with a value whose ToString() is not an integer");
			#endregion

			seconds = int.Parse(value.ToString());

			System.Windows.MessageBox.Show(String.Format("Converting string to seconds: {0}", seconds));
			return new TimeSpan(0, 0, seconds);
		}


		/// <summary>
		/// Convert a timespan's seconds back to a string
		/// </summary>
		/// <param name="value">Not Used</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>Not Used</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			TimeSpan span = (TimeSpan)value;
			return span.Seconds.ToString();
		}

	}
}
